package ui;

import dao.UserDAO;

import javax.swing.*;
import java.awt.*;

public class LoginFrame extends JFrame {
    public LoginFrame() {
        setTitle("管理员登录");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JLabel userLabel = new JLabel("用户名:");
        JTextField userField = new JTextField(15);
        JLabel passLabel = new JLabel("密码:");
        JPasswordField passField = new JPasswordField(15);
        JButton loginBtn = new JButton("登录");

        JPanel panel = new JPanel(new GridLayout(3, 2));
        panel.add(userLabel); panel.add(userField);
        panel.add(passLabel); panel.add(passField);
        panel.add(new JLabel()); panel.add(loginBtn);

        loginBtn.addActionListener(e -> {
            String user = userField.getText();
            String pass = new String(passField.getPassword());
            if (new UserDAO().login(user, pass)) {
                JOptionPane.showMessageDialog(this, "登录成功");
                new AdminFrame();
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "用户名或密码错误");
            }
        });

        add(panel);
        setVisible(true);
    }
}
